/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
//import javax.sql.DataSource;

/**
 *
 * @author Alberto
 */
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {
       @Resource(name = "jdbc/Tienda_bfernandezo_asanchezoliv")
       private DataSource ds;
    private String contenido = "";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
         ServletContext context = this.getServletContext();
            
            out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            out.println("<html xmlns='http://www.w3.org/1999/xhtml' >");
            out.println("<head>");
            out.println("<link href='styles/styles.css' rel='stylesheet' type='text/css'>");
            out.println("<title> Login </title>");
            out.println("</head>");
            out.println("<body class='Login'>");

            out.println("<h1 id='titulo'>Login En ca' Paqui</h1>");
            out.println(contenido);
            out.println("<div class='login'>");
            out.println("<form method='post' action='Login'>");
            out.println("Usuario: <input type='text' name='u' /><br/>");
            out.println("Password: <input type='password' name='p' />");
            out.println("<input type='submit' name='Enviar' value='Enviar' class='boton_enviar'>"); 
            out.println("</form>");
            out.println("<div class='atras'><a href='/Tienda_bfernandezo_asanchezoliv/'>Atrás</a><div>");
            out.println("</div>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
            
            
           /* 
            context.setAttribute("datosAdmin", datosAdmininstrador);
            String usuario= request.getParameter("usuario");
            context.setAttribute("user", usuario);
            String password= request.getParameter("password");
            */
            //System.out.println("Usuario:"+usuario);
            //System.out.println("Password:"+password);context.setAttribute("pass", password);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
   
    }
    

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try{
            
            Tienda tienda = Tienda.getInstancia(ds);
            
            String[] usuario = tienda.getUsuario(request.getParameter("u"),request.getParameter("p"));
          
            System.out.println(usuario[0]);
            if (usuario[0] != null){
                if (usuario[0].toUpperCase() == "ADMIN")
                    response.sendRedirect( "/Tienda_bfernandezo_asanchezoliv/Login" );
                else
                    response.sendRedirect( "/Tienda_bfernandezo_asanchezoliv/admin.jsp" );
            }                
        }catch(Exception e){System.out.println(e.getMessage());}
        contenido ="";
        contenido +="<h3 id='titulo'>Usuario y/o contraseña incorrectos.</h3>";
        processRequest(request, response);
        
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
